package cn.com.ylpw.web.special.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.ylpw.core.Context;
import cn.com.ylpw.spring.SpringController;
import cn.com.ylpw.utils.StringUtils;
import cn.com.ylpw.web.product.service.GroupSpecialService;
import cn.com.ylpw.web.product.service.IProductService;
import cn.com.ylpw.web.product.service.IVenuesService;

import com.google.common.collect.Maps;
/**
 * 聚合专题Controller
 * 
 * @author July july_sky@foxmail.com
 * @date 2015年5月29日下午5:26:14
 * @Copyright ©2003-2015 北京春秋永乐文化传播有限公司. All Rights Reserved.
 * @version 1.0
 */
@Controller("groupSpecialController")
public class GroupSpecialController extends SpringController {
	 public final Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	@Qualifier("venuesService")
	private IVenuesService venuesService;
	@Autowired
	@Qualifier("productService")
	private IProductService productService;
	@Autowired
	@Qualifier("groupSpecialService")
	private GroupSpecialService groupSpecialService;	
	
	/**
	 * 场馆聚合页
	 * 
	 * @author July july_sky@foxmail.com
	 * @date 2015年6月1日下午5:49:10
	 * @param cityjc
	 * @param venuesid
	 * @param params
	 * @return
	 */
	@RequestMapping("/{cityjc:[a-zA-Z]+}/ven-{venuesid:[0-9]+}")
	public ModelAndView getVenues(@PathVariable("cityjc") String cityjc,
			@PathVariable("venuesid") Integer venuesid,
			@RequestParam Map<String, Object> params) {
		return getVenuesType(cityjc, venuesid, -1, params);
	}
	/**
	 * 场馆聚合页，包含分类
	 * 
	 * @author July july_sky@foxmail.com
	 * @date 2015年6月2日上午10:11:13
	 * @param cityjc 城市简称
	 * @param venuesid 场馆ID
	 * @param producttypeaid1 商品分类ID
	 * @param params
	 * @return
	 */
	@RequestMapping("/{cityjc:[a-zA-Z]+}/ven-{venuesid:[0-9]+}/{producttypeaid1:[0-9]+}")
	public ModelAndView getVenuesType(@PathVariable("cityjc") String cityjc,
			@PathVariable("venuesid") Integer venuesid,
			@PathVariable("producttypeaid1") Integer producttypeaid1,
			@RequestParam Map<String, Object> params) {
		if(producttypeaid1 > 1000){
			return getVenuesTypePage(cityjc, venuesid, producttypeaid1, 1, params);
		}
		return getVenuesTypePage(cityjc, venuesid, -1, producttypeaid1, params);
	}
	
	/**
	 * 场馆聚合页，包含分类、分页
	 * 
	 * @author July july_sky@foxmail.com
	 * @date 2015年6月2日上午10:11:13
	 * @param cityjc 城市简称
	 * @param venuesid 场馆ID
	 * @param producttypeaid1 商品分类ID
	 * @param params
	 * @return
	 */
	@RequestMapping("/{cityjc:[a-zA-Z]+}/ven-{venuesid:[0-9]+}/{producttypeaid1:[0-9]+}/{curPage:[0-9]+}")
	public ModelAndView getVenuesTypePage(@PathVariable("cityjc") String cityjc,
			@PathVariable("venuesid") Integer venuesid,
			@PathVariable("producttypeaid1") Integer producttypeaid1,
			@PathVariable("curPage") Integer curPage,
			@RequestParam Map<String, Object> params) {
		ModelAndView mv = new ModelAndView();
		super.clean(params);
		params.put("venuesid", venuesid);
		try {
			/**
			 * 加载场馆信息
			 */
			mv.addObject("venue",venuesService.getVenues(params));
			/**
			 * 加载场馆近期演出安排信息
			 */
			params.put(Context.LIMIT_START_FIELD, 1);
			params.put(Context.LIMIT_END_FIELD, 10);
			params.put("allStatus", "0,1");
			params.put("display", 1);
			mv.addObject("newProductList",productService.findProductList(params));
			
			/**
			 * 加载场馆商品分类商品列表
			 */
			Map<String,Object> query = Maps.newHashMap();
			super.clean(query);
			query.put("venuesid", venuesid);
			query.put(Context.PAGE_SIZE_FIELD, 12);
			query.put(Context.LIMIT_START_FIELD, curPage);
			if(producttypeaid1 > -1){
				query.put("producttypeaid1",producttypeaid1);
			}
			query.put("display", 1);
			query.put("allStatus", "0,1");
			mv.addObject("pageResult",productService.getProductPageResult(query));
			/**
			 * 加载场馆热卖商品
			 */
			query = Maps.newHashMap();
			query.put("venuesid", venuesid);
			query.put(Context.LIMIT_END_FIELD, 4);
			query.put(Context.LIMIT_START_FIELD, 1);
			mv.addObject("hotProductList",productService.getProductByVenuesHotList(query));
			/**
			 * 加载热门场馆
			 */
			mv.addObject("hotVenueList",venuesService.findVenuesByHotList(query));
			
			mv.addObject("venuesid",venuesid);
			mv.addObject("producttypeaid1",producttypeaid1);
			mv.addObject("cityjc",cityjc);
		} catch (Exception e) {
			log.warn("加载场馆聚合页相关信息失败{}",params, e);
		}
		mv.setViewName("subunion/venues");
		return mv;
	}
	
	
	/**
	 * 演唱会聚合页，包含分类、分页
	 * 
	 * @author yuwang
	 * @date 2015年6月24日 15:03:51
	 * @param cityjc 城市简称
	 * @param productid 商品ID
	 * @param producttypeaid1 商品分类ID
	 * @param params
	 * @return
	 */
	//@Cacheable(key="#root.targetClass.name+#root.methodName+'_'+#artistid",value="ehcache#600")
	@RequestMapping("/yanchanghui/{artistid:[0-9]+}")
	public ModelAndView getConcertTypePage(@PathVariable("artistid") String artistid,
			@RequestParam Map<String, Object> params) {
		ModelAndView mv = new ModelAndView();
		super.clean(params);
		try {
			/**
			 * 加载全部明星资讯
			 */		
			List<Map<String,Object>> AllNewsList = groupSpecialService.getArtistNews(params);
			List<Map<String,Object>> AllNewsList1 = new ArrayList<Map<String, Object>>();
			List<Map<String,Object>> AllNewsList2 = new ArrayList<Map<String, Object>>();
			List<Map<String,Object>> AllNewsList3 = new ArrayList<Map<String, Object>>();
			List<Map<String,Object>> AllNewsList4 = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < AllNewsList.size(); i++) {
				if (i == 0) {
					AllNewsList1.add(AllNewsList.get(i));
				}else if(i >0 && i<5){
					AllNewsList2.add(AllNewsList.get(i));
				}else if(i >=5 && i<7){
					AllNewsList3.add(AllNewsList.get(i));
				}else if(i >=7 && i<10){
					AllNewsList4.add(AllNewsList.get(i));
				}
			}
			mv.addObject("AllNewsList1",AllNewsList1);
			mv.addObject("AllNewsList2",AllNewsList2);
			mv.addObject("AllNewsList3",AllNewsList3);
			mv.addObject("AllNewsList4",AllNewsList4);
			params.put("artistid", artistid);
			/**
			 * 加载艺人信息
			 */
			mv.addObject("artist",groupSpecialService.getArtistInfo(params));
			/**
			 * 加载演唱会门票
			 */
			mv.addObject("productPriceList",groupSpecialService.getCusPerform(params));
			/**
			 * 加载明星的所有演唱会
			 */
			params.put("producttypeaid", "142450");
			mv.addObject("cusConcert",groupSpecialService.getCusConcert(params));
			List<Map<String,Object>> counTLinkList = groupSpecialService.getCounTLink(params);
			//94 全国巡演	95全国首页即将开票	97全国首页精彩专题
			//全国巡演
			List<Map<String,Object>> tourList = new ArrayList<Map<String, Object>>();
			//全国首页即将开票
			List<Map<String,Object>> soonList = new ArrayList<Map<String, Object>>();
			//即将上演
			List<Map<String,Object>> soonShowList = new ArrayList<Map<String, Object>>();
			//全国首页精彩专题
			List<Map<String,Object>> subjectList = new ArrayList<Map<String, Object>>();

			for (Map<String,Object> map : counTLinkList) {
				if (!StringUtils.isBlank(map.get("TYPE"))) {
					if (map.get("TYPE").toString().equals("94")) {
						tourList.add(map);
					}
					if (map.get("TYPE").toString().equals("95")) {
						soonList.add(map);
					}	
					if (map.get("TYPE").toString().equals("97")) {
						subjectList.add(map);
					}	
				}

			}
			soonShowList =groupSpecialService.getCouSoonShowList(params); 
			mv.addObject("tourList",tourList);
			mv.addObject("soonList",soonList);
 			mv.addObject("soonShowList",soonShowList);
			mv.addObject("subjectList",subjectList);
			
			/**
			 * 加载明星资讯
			 */		
			List<Map<String,Object>> artistNewsList = groupSpecialService.getArtistNews(params);
			mv.addObject("artistNewsList",artistNewsList);
			/**
			 * 加载艺人行程安排
			 */
			params.put("islimit", "no");
			mv.addObject("performList",groupSpecialService.getCusPerform(params));
		} catch (Exception e) {
			log.warn("加载艺人详情页相关信息失败{}",params, e);
		}
		
		mv.setViewName("subunion/starconcert");
		return mv;
	}	
}
